﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace YLClient.UserControls
{
    /// <summary>
    /// Interaction logic for TimePick.xaml
    /// </summary>
    public partial class TimePick : UserControl
    {
        public TimePick()
        {
            InitializeComponent();
            var now = DateTime.Now;
            Enumerable.Range(now.Year - 10, 20).ToList()
                .ForEach(i =>
                    cmbY.Items.Add(i)
                );
            Enumerable.Range(1, 12).ToList()
                .ForEach(i =>
                    cmbM.Items.Add(i)
                );
            Enumerable.Range(0, 24).ToList()
                .ForEach(i =>
                    cmbH.Items.Add(i)
                );
            this.Time = DateTime.Now;

            cmbY.SelectedIndex = 10;
            cmbM.SelectedIndex = DateTime.Now.Month - 1;
            cmbD.SelectedIndex = DateTime.Now.Day - 1;
            cmbH.SelectedIndex = DateTime.Now.Hour - 1;
        }
        DateTime time;
        public DateTime Time
        {
            get
            {
                var y = (int)cmbY.SelectedValue;
                var m = (int)cmbM.SelectedValue;
                var d = (int)cmbD.SelectedValue;
                var h = (int)cmbH.SelectedValue;
                return new DateTime(y, m, d, h, 59, 59);
            }
            set
            {
                cmbY.SelectedIndex = value.Year - DateTime.Now.Year;
                cmbM.SelectedIndex = value.Month - 1;
                cmbD_GotFocus(null, null); // 初始化时间
                cmbD.SelectedIndex = value.Day - 1;
                cmbH.SelectedIndex = value.Hour - 1;
            }
        }
        bool resetTime;
        private void cmbD_GotFocus(object sender, RoutedEventArgs e)
        {
            if (resetTime == false)
            {
                return;
            }
            cmbD.Items.Clear();
            var y = (int)cmbY.SelectedValue;
            var m = (int)cmbM.SelectedValue;
            int days = 0;
            bool leapYear = false;
            if (y % 100 == 0 && y % 400 == 0)
            {
                leapYear = true;
            }
            else if (y % 4 == 0)
            {
                leapYear = true;
            }
            switch (m)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31; break;
                case 2:
                    if (leapYear)
                        days = 29;
                    else
                        days = 28;
                    break;

                default:
                    days = 30;
                    break;
            }
            Enumerable.Range(1, days).ToList()
                .ForEach(i =>
                    cmbD.Items.Add(i)
                );
            resetTime = false;
        }

        private void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            resetTime = true;
        }
    }
}
